# -*- coding: utf-8-*-
from openerp.osv import osv, fields


# 微信权限组
class wechat_permission_group(osv.osv):
    _name = 'wechat.permission.group'
    _description = u"微信权限组"

    _columns = {
        'name': fields.char(u'名称'),
        'code': fields.char(u'内部编码'),
        'enterprise_id': fields.many2one('wechat.enterprise', u'企业号'),
        'secret': fields.char(u'开发者凭据密钥'),  # 微信企业号->设置->权限管理->某接口调用权限组->开发者凭据密钥Secret
        'access_token': fields.char(u'微信接口主动调用token'),  # 7200秒有
        'access_token_timestamp': fields.integer(u'token起始时间戳'),
        'jsapi_ticket': fields.char(u'微信js sdk访问token'),  # 7200秒有效
        'jsapi_ticket_timestamp': fields.integer(u'js sdk token起始时间戳'),
        'application_ids': fields.many2many('wechat.application', 'application_permission_ref', 'permission_id', 'application_id', u'应用权限'),
        'is_authenticate': fields.boolean(u'认证组'),  # 拥有全用户的授权标记，去这个的access_token去进行用户相关接口操作肯定没错
    }

    def get_access_token_by_code(self, cr, uid, code, context=None):
        group_ids = self.search(cr, uid, [('code', '=', code)], limit=1, order='id', context=context)

        if group_ids:
            vals = self.read(cr, uid, group_ids[0], ['access_token'], context=context)
            if vals.get('access_token'):
                return vals.get('access_token')

            raise osv.except_osv(u'错误', u'该编码所对应的组还没有获得到正确的 access_token')

        raise osv.except_osv(u'错误', u'不存在一个该编码所对应的组')

    def get_authenticate_access_token(self, cr, uid, context=None):
        group_id = self.get_authenticate_group(cr, uid, context=context)

        vals = self.read(cr, uid, group_id, ['access_token'], context=context)
        if vals.get('access_token'):
            return vals.get('access_token')

        raise osv.except_osv(u'错误', u'认证组还没有获得到正确的 access_token')

    def get_authenticate_group(self, cr, uid, context=None):
        group_ids = self.search(cr, uid, [('is_authenticate', '=', True)], limit=1, order='id', context=context)
        if group_ids:
            return group_ids[0]

        raise osv.except_osv(u'错误', u'不存在一个认证组的权限组')
